#!/usr/bin/python
# -*- coding: utf-8 -*-
import maya.cmds as mc
import maya.OpenMaya as om
from objectName import Naming

class Hierarchy(Naming):
    
    def makeObjZero(self,objName=None,keepParent=True,tlt=True,rote=True,objType="transform",upDn="up",suffix="_Zero"):
        result = []
        if objName==None and mc.ls(sl=True)!=[]:
            objName=mc.ls(sl=True)
        if type(objName)==str or type(objName)==unicode:
            objName = [objName]
        #make help object
        for o in objName:
            transName = self.getLastName(o)
            mc.select(cl=True)
            trans = mc.createNode(objType,name="%s%s"%(transName,suffix))
            result.append(trans)
            if tlt==True:
                t = mc.xform(o,q=1,ws=1,t=1)
                mc.xform(trans,ws=1,t=t)
            if rote==True:
                #you must use orientConstraint to set the rotate.becasure the different rotateOrder make xform different value.
                mc.delete(mc.orientConstraint(o,trans,offset=[0,0,0],weight=1))
                #ro = mc.xform(o,q=1,ws=1,ro=1)
                #mc.xform(trans,ws=1,ro=ro)
            #deel with parent
            #get it's parent obj name
            itsParent = mc.listRelatives(o,parent=True)#,fullPath=True)
            #get it's child obj name
            itsChild = mc.listRelatives(o,children=True,type=["transform","joint"])
            if upDn=='up':
                mc.parent(o,trans)
                if itsParent!=None:
                    mc.parent(trans,itsParent)
            elif upDn=='dn':
                mc.parent(trans,o)
                if itsChild!=None:
                    mc.parent(itsChild,trans)
            else:
                self.mayaError("upDn must input 'up' or 'dn' string.")
                return
            """
            if keepParent==True:
                    mc.parent(objName,trans)
                    if itsParent!=None:
                            mc.parent(trans,itsParent[0])
            else:
                    mc.parent(objName,trans)"""
        if result!=[]:
            mc.select(result)
        return result
        
    def makeDnGroup(self,objectName,type_="transform",name="Hlp",alone=False):
        #at point object build a grp for animator key keyframe
        dnObj = mc.listRelatives(objectName,children=True)
        if type_=="joint":
            mc.select(objectName)
            ctlHlp = mc.joint(name=name)
        else:
            ctlHlp = mc.createNode(type_,p=objectName,name=name)
        if alone!=True and dnObj!=None:
            for x in dnObj:
                mc.parent(x,ctlHlp,a=True)
        return ctlHlp
        
    def makeUpGroup(self,objectName,type_="transform",name="Hlp",alone=False):
        """#at point object build a grp for animator key keyframe
        #v1.01 the first version"""
        dnObj = mc.listRelatives(objectName,children=True)
        if type_=="joint":
            mc.select(objectName)
            ctlHlp = mc.joint(name=name)
        else:
            ctlHlp = mc.createNode(type_,p=objectName,name=name)
        if alone!=True and dnObj!=None:
            for x in dnObj:
                mc.parent(x,ctlHlp,a=True)
        return ctlHlp
    
    def howArrange(self,*aList):
        #parent method form a list,next parent to last;
        for i in range(0,len(aList)-1,1):
            mc.parent(aList[i+1],aList[i])
            
    def parentShapeAndRename(self):
        obj = mc.ls(sl=True)
        if len(obj)%2!=0:
            self.mayaWarning("U must select a  pair of objects Shape and Target.")
            return
        for i in range(0,len(obj),2):
            shapeFrom = mc.listRelatives(obj[i],shapes=True)
            if shapeFrom!=None:
                mc.parent(shapeFrom,obj[i+1],r=True,s=True)
                newName = mc.rename(shapeFrom[0],"%sShape"%(obj[i+1]))
                mc.delete(obj[i])
                mc.select(obj[i+1])
                self.mayaPrint("Parent shape %s to %s and rename it %s."%(shapeFrom[0],obj[i+1],newName),)
                
hierarchyCls = Hierarchy()
